package com.lovo.zuoye;

import javax.swing.text.AbstractDocument;
import java.io.*;
import java.util.Scanner;

public class Zy {

    // 拷贝
    public void Copy() {
        // 创建字节流对象
        FileInputStream fis = null;
        FileOutputStream opw = null;
        try {
            // 循环读取数据
            fis = new FileInputStream("C:\\Users\\ASUS\\Desktop\\a.txt");
            opw = new FileOutputStream("C:\\Users\\ASUS\\Desktop\\b.txt");
            int b = 0;
            byte[] bs = new byte[1024];
            while ((b = fis.read(bs)) != -1) {
                opw.write(bs, 0, b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    // 关闭流
                    fis.close();
                }
                if (opw != null) {
                    opw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    // 追加
    public void Append(String filename, String a) {
        RandomAccessFile randomFile = null;
        try {
            randomFile = new RandomAccessFile(filename, "rw");
            randomFile.seek(randomFile.length());
            randomFile.write(a.getBytes("gb2312"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//    // 追加
//    public void Append(String filename, String a) {
//        FileWriter w = null;
//        try {
//            w = new FileWriter(filename, true);
//            char[] b = a.toCharArray();
//            for (int i = 0; i < b.length; i++) {
//                w.append(b[i]);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (w != null) {
//                try {
//                    w.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Zy z = new Zy();
//        z.Copy();
        System.out.println("请用户输入：");
        String a = sc.next();
        z.Append("C:\\Users\\ASUS\\Desktop\\a.txt", a);
    }
}
